package com.wwinsoft.framework.service.security;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.wwinsoft.framework.dao.security.ResourceDao;
import com.wwinsoft.framework.entity.security.Resource;

@Component
@Transactional
public class ResourceDetailsService {
    public String URL = "url";
    public String METHOD = "method";
    public String AUTH_NAMES = "authNames";

    @Autowired
	private ResourceDao resourceDao;

	//-- Spring Security Filter --//
    @Transactional(readOnly = true)
    public List<Map<String,Object>> getRequestMap() {
        List<Resource> resourceWithAuthorities = resourceDao.getUrlResourceWithAuthorities();

        List<Map<String,Object>> requestMapList = Lists.newArrayList();
        for (Resource resource : resourceWithAuthorities) {
            Map<String, Object> map = Maps.newHashMap();
            map.put("url",resource.getUrl());
            map.put("method",resource.getMethod());
            map.put("authNames",resource.getAuthNamesArray());

            requestMapList.add(map);
        }

        return requestMapList;
    }

}
